<?php

defined('IN_PHPCMS') or exit('No permission resources.');
if (!defined('CACHE_MODEL_PATH')) define('CACHE_MODEL_PATH', PHPCMS_PATH . 'caches' . DS . 'caches_model' . DS . 'caches_data' . DS);

pc_base::load_sys_class('model', '', 0);

class member_model extends model {

    public function __construct() {
        $this->db_config = pc_base::load_config('database');
        $this->db_setting = 'default';
        $this->table_name = 'member';
        parent::__construct();
    }

    /**
     * 重置模型操作表表
     * @param string $modelid 模型id
     */
    public function set_model($modelid = '') {
        if ($modelid) {
            $model = getcache('member_model', 'commons');
            if (isset($model[$modelid])) {
                $this->table_name = $this->db_tablepre . $model[$modelid]['tablename'];
            } else {
                $this->table_name = $this->db_tablepre . $model[10]['tablename'];
            }
        } else {
            $this->table_name = $this->db_tablepre . 'member';
        }
    }

    const TYPE_LOGIN_FRIST = 1;//首次登录
    const TYPE_FULL_INFO = 2;//填写了完整信息

    const TYPE_SIGN = 11;//签到
    const TYPE_LIKE = 12;
    const TYPE_SHARE = 13;
    const TYPE_YP_1 = 14;//被填
    const TYPE_YP_2 = 15;//填写
    const TYPE_COMMIT = 16;//评论

    const TYPE_APPLY = 21;//报名
    const TYPE_SHOP = 22;//兑换

    //积分经验操作
    public static function log($uid, $price, $type, $content, $mca = '') { 

        $db_jifen_log = pc_base::load_model('jifen_log_model');
        $info = [
            'uid' => $uid,
            'price' => $price,
            'type' => $type,
            'content' => $content,
            'mca' => $mca,
            'ip' => ip(),
            'ymd' => date('Y-m-d'),
            'ymdhis' => date('Y-m-d H:i:s'),
        ];
        $db_jifen_log->insert($info);

        $db_member = new self;
        if ($price >= 0) {
            $price = '+' . $price;
        } else {
            $info = $db_member->get_one(['userid' => $uid]);
            $cha = abs($price) - intval($info['jifen']);
            if ($cha > 0) {
                return $cha;
            }
        }

        $userinfo = $db_member->get_one(['userid' => $uid]);
        pc_base::load_sys_class('exp');        
        $old_lv=exp::lv($userinfo['exp']);
        $old_jifen=intval($userinfo['jifen']/500);
        //echo $old_jifen;exit();
        if (in_array($type, [self::TYPE_SIGN, self::TYPE_LIKE, self::TYPE_SHARE, self::TYPE_YP_1, self::TYPE_YP_2, self::TYPE_COMMIT])) {
            $db_exp_log = pc_base::load_model('exp_log_model');
            $db_exp_log->insert($info);

            $sql = "update {$db_member->table_name} set jifen=jifen$price,exp=exp$price where userid=$uid";
        } else {
            $sql = "update {$db_member->table_name} set jifen=jifen$price where userid=$uid";
        }
        $db_member->query($sql);
        $userinfo = $db_member->get_one(['userid' => $uid]);
        pc_base::load_sys_class('exp');        
        $new_lv=exp::lv($userinfo['exp']);
        //等级提升
        if ($new_lv>$old_lv) {
            $db_member->fxx($uid,'亲，您的学分等级已提升，恭喜您已成为LV'.$new_lv.'天使会员，详见“高见学社订阅号”。');
        }
        if (intval($userinfo['jifen']/500)>$old_jifen) {
            $db_member->fxx($uid,'亲，恭喜您的学分已到'.$userinfo['jifen'].'，详见“高见学社订阅号”。');
        }
        return 0;
    }
    public static function fxx($uid,$con) {
        $reminds = pc_base::load_model('reminds_model');
        $info = [
            'uid' => $uid,
            'title' => $con,
            'type' => 1,
            'small_content' => $con,
            'readed' => 0,            
            'ymd' => date('Y-m-d'),
            'ymdhis' => date('Y-m-d H:i:s'),
        ];
        $reminds->insert($info);
        $db_member = new self;
        $userinfo = $db_member->get_one(['userid' => $uid]);
        $params = [
            'name' => 'dxwgjxs',     //必填参数。用户账号 123456789a
            'pwd' => '85DAB3A663119F52C3B4BDC24478',     //必填参数。（web平台：基本资料中的接口密码）
            'content' => $con,   //必填参数。发送内容（1-500 个汉字）UTF-8编码
            'mobile' => $userinfo['mobile'],   //必填参数。手机号码。多个以英文逗号隔开
            'stime' => '',   //可选参数。发送时间，填写时已填写的时间发送，不填时为当前时间发送
            'sign' => '高见学社',    //必填参数。用户签名。
            'type' => 'pt',  //必填参数。固定值 pt
            'extno' => ''    //可选参数，扩展码，用户定义扩展码，只能为数字
        ];
        
        $params = http_build_query($params);

        $url = "http://web.duanxinwang.cc/asmx/smsservice.aspx?" . $params; //提交的url地址

        $res = file_get_contents($url);  //获取信息发送后的状态
    }

}